﻿using UnityEngine;
using System.Collections.Generic;

public class MyTouch
{
	public MyTouch()
	{
	}
	public MyTouch(Touch touch)
	{
		fingerId = touch.fingerId;
		position = touch.position;
	}
	public int fingerId;
	public Vector3 position;
}

public class TouchContent
{
	public static TouchContent CreateFromTouch(MyTouch[] touch)
	{
		if (touch.Length == 2)
		{
			return new TouchContent (touch);
		}
		return null;
	}
	public TouchContent(MyTouch[] touch)
	{
		touchs.AddRange( touch );
	}
	private List<MyTouch> BuildTouch(MyTouch[] touch)
	{
		var list = new List<MyTouch> ( touch );
		list.Sort ((MyTouch a, MyTouch b) =>
		{
			return a.fingerId < b.fingerId ? -1 : 1;
		});
		return list;
	}
	public bool Test(MyTouch[] touch)
	{
		if (touch.Length != 2)
		{
			return false;
		}
		var newTouchGroup = BuildTouch (touch);
		if (touchs [0].fingerId != newTouchGroup [0].fingerId
			|| touchs [1].fingerId != newTouchGroup [1].fingerId)
		{
			return false;
		}

		touchs = newTouchGroup;
		return true;
	}

	public List<MyTouch> touchs = new List<MyTouch>();
}
public class InputMngr : MonoBehaviour
{
	public Camera uiCamera;
	public float size = 100f;
	public GameObject prefab;
	public UISprite sprite1, sprite2;
	void Start()
	{
		sprite1.gameObject.SetActive (false);
		sprite2.gameObject.SetActive (false);
	}
	public static bool isMultiTouch
	{
		get;
		private set;
	}
	private MyTouch[] currentTouchs
	{
		get;
		set;
	}
	private void VirtualTouch()
	{
		if (Input.GetMouseButtonDown (1))
		{
			Debug.Log ("MouseButtonDown");
			m_beginPosition = Input.mousePosition;
			m_touched = true;
		}
		if (Input.GetMouseButtonUp (1))
		{
			m_touched = false;
			currentTouchs = new MyTouch[0];
		}
		if (m_touched)
		{
			currentTouchs = new MyTouch[2];
			Vector3 dir = Input.mousePosition - m_beginPosition;

			currentTouchs [0] = new MyTouch ();
			currentTouchs [0].fingerId = 0;
			currentTouchs [0].position = m_beginPosition + dir;
			currentTouchs [1] = new MyTouch ();
			currentTouchs [1].fingerId = 1;
			currentTouchs [1].position = m_beginPosition - dir;
		}
	}
	private bool m_touched = false;
	private Vector3 m_beginPosition;
	void Update()
	{
		currentTouchs = new MyTouch[Input.touches.Length];
		for (int i = 0; i < currentTouchs.Length; ++i)
		{
			currentTouchs [i] = new MyTouch( Input.touches [i] );
		}

		VirtualTouch ();


		if (m_content == null)
		{
			m_content = TouchContent.CreateFromTouch (currentTouchs);
			if (m_content != null)
			{
				Open ();
				Sync ();
				isMultiTouch = true;
			}
		}
		else
		{
			if (m_content.Test (currentTouchs))
			{
				Sync ();
			}
			else
			{
				m_content = null;
				Close ();
				isMultiTouch = false;
			}
		}
	}
	private void Open()
	{
		sprite1.gameObject.SetActive (true);
		sprite2.gameObject.SetActive (true);
	}
	private void Close()
	{
		sprite1.gameObject.SetActive (false);
		sprite2.gameObject.SetActive (false);
	}
	private void Sync()
	{
		position [0] = TouchToLocalPos (m_content.touchs [0].position);
		position [1] = TouchToLocalPos (m_content.touchs [1].position);
		sprite1.transform.localPosition = position [0];
		sprite2.transform.localPosition = position [1];
	}
	public static Vector3[] position = new Vector3[2];
	private Vector3 TouchToLocalPos(Vector3 pos)
	{
		var worldPos = uiCamera.ScreenToWorldPoint (pos);
		return transform.worldToLocalMatrix.MultiplyPoint (worldPos);
	}
	void OnDrawGizmos()
	{
		var worldPos = uiCamera.ScreenToWorldPoint( Input.mousePosition );
		var localPosition = transform.worldToLocalMatrix.MultiplyPoint (worldPos);
		Gizmos.matrix = transform.localToWorldMatrix;
		Gizmos.DrawWireSphere ( localPosition, 1f );
//		prefab.transform.localPosition = localPosition;
	}
	private TouchContent m_content = null;
}
